import { config } from '@/service/config'
import { MockMethod } from 'vite-plugin-mock'

const { result_code } = config

const timeout = 300

const List: {
  username: string
  password: string
  role: string
  roleId: string
  permissions: string | string[]
}[] = [
  {
    username: 'admin',
    password: 'admin',
    role: 'admin',
    roleId: '1',
    permissions: ['*.*.*']
  },
  {
    username: 'test',
    password: 'test',
    role: 'test',
    roleId: '2',
    permissions: ['demo:form:create', 'demo:form:delete']
  }
]

const routers = [
  {
    path: '/demo',
    component: '#',
    redirect: '/demo/menu1/menu1-1/demo-menu1-1-1',
    name: 'Level',
    meta: {
      title: 'formDemo.example',
      icon: 'svg-icon:message'
    },
    children: [
      {
        path: 'menu1',
        name: 'Menu1',
        component: '##',
        redirect: '/demo/menu1/menu1-1/menu1-1-1',
        meta: {
          title: 'router.menu1'
        },
        children: [
          {
            path: 'menu1-1',
            name: 'Menu11',
            component: '##',
            redirect: '/demo/menu1/menu1-1/menu1-1-1',
            meta: {
              title: 'router.menu11',
              alwaysShow: true
            },
            children: [
              {
                path: 'menu1-1-1',
                name: 'Menu111',
                component: 'views/demo/demo-menu111',
                meta: {
                  title: 'router.menu111'
                }
              }
            ]
          },
          {
            path: 'menu1-2',
            name: 'Menu12',
            component: 'views/demo/demo-menu12',
            meta: {
              title: 'router.menu12'
            }
          },
          {
            path: 'menu1-3',
            name: 'Menu13',
            component: '#iframe',
            meta: {
              title: 'levelDemo.iframe',
              link: 'https://eslint.vuejs.org/rules/'
            }
          }
        ]
      },
      {
        path: 'table',
        name: 'TableDemo',
        component: 'views/demo/table-demo',
        meta: {
          title: 'router.table'
        }
      },
      {
        path: 'form',
        name: 'FormDemo',
        component: 'views/demo/form-demo',
        meta: {
          title: 'router.form'
        }
      },
      {
        path: 'material-control-dbt-view',
        name: 'materialControlDbtView',
        component: 'views/demo/material-control-dbt-view',
        meta: {
          title: 'router.materialControlDbtView'
        }
      },
      {
        path: 'demand',
        name: 'Demand',
        component: '#',
        meta: {
          title: 'router.demand'
        },
        redirect: '/demo/demand/view-page',
        children: [
          {
            path: 'view-page',
            name: 'ViewPage',
            component: 'views/demand/view-page',
            meta: {
              title: 'router.view'
            }
          },
          {
            path: 'adjust-page',
            name: 'AdjustPage',
            component: 'views/demand/adjust-page',
            meta: {
              title: 'router.adjust'
            }
          }
        ]
      },
      {
        path: 'template',
        name: 'Template',
        component: '#',
        redirect: '/demo/template/template-one',
        meta: {
          title: 'router.template'
        },
        children: [
          {
            path: 'template-one',
            name: 'TemplateOne',
            component: 'views/template-one/template-one',
            meta: {
              title: 'router.templateOne'
            }
          }
        ]
      }
    ]
  }
]

const i18n = {
  'zh-CN': {
    formDemo: {
      input: '输入框',
      inputNumber: '数字输入框',
      default: '默认',
      icon: '图标',
      mixed: '复合型',
      textarea: '多行文本',
      slot: '插槽',
      position: '位置',
      autocomplete: '自动补全',
      select: '选择器',
      selectGroup: '选项分组',
      selectV2: '虚拟列表选择器',
      cascader: '级联选择器',
      switch: '开关',
      rate: '评分',
      colorPicker: '颜色选择器',
      transfer: '穿梭框',
      render: '渲染器',
      radio: '单选框',
      button: '按钮',
      checkbox: '多选框',
      slider: '滑块',
      datePicker: '日期选择器',
      shortcuts: '快捷选项',
      today: '今天',
      yesterday: '昨天',
      aWeekAgo: '一周前',
      week: '周',
      year: '年',
      month: '月',
      dates: '日期',
      daterange: '日期范围',
      monthrange: '月份范围',
      dateTimePicker: '日期时间选择器',
      dateTimerange: '日期时间范围',
      timePicker: '时间选择器',
      timeSelect: '时间选择',
      inputPassword: '密码输入框',
      passwordStrength: '密码强度',
      defaultForm: '全部示例',
      formDes: '基于 ElementPlus 的 base-form 组件二次封装，实现数据驱动，支持所有 base-form 参数',
      example: '示例',
      operate: '操作',
      change: '更改',
      restore: '还原',
      disabled: '禁用',
      disablement: '解除禁用',
      delete: '删除',
      add: '添加',
      setValue: '设置值',
      resetValue: '重置值',
      set: '设置',
      subitem: '子项',
      formValidation: '表单验证',
      verifyReset: '验证重置'
    },

    levelDemo: {
      menu: '多级菜单缓存',
      iframe: '外部页面'
    },
    searchDemo: {
      dynamicOptions: '动态选项'
    },
    tableDemo: {
      table: '表格',
      tableDes: '基于 ElementPlus 的 base-table 组件二次封装',
      index: '序号',
      title: '标题',
      author: '作者',
      displayTime: '创建时间',
      importance: '重要性',
      pageviews: '阅读数',
      action: '操作',
      important: '重要',
      good: '良好',
      commonly: '一般',
      operate: '操作',
      example: '示例',
      show: '显示',
      hidden: '隐藏',
      pagination: '分页',
      reserveIndex: '叠加序号',
      restoreIndex: '还原序号',
      showSelections: '显示多选',
      hiddenSelections: '隐藏多选',
      showExpandedRows: '显示展开行',
      hiddenExpandedRows: '隐藏展开行',
      changeTitle: '修改标题',
      header: '头部',
      selectAllNone: '全选/全不选'
    }
  },
  en: {
    formDemo: {
      input: 'Input',
      inputNumber: 'InputNumber',
      default: 'Default',
      icon: 'Icon',
      mixed: 'Mixed',
      textarea: 'Textarea',
      slot: 'Slot',
      position: 'Position',
      autocomplete: 'Autocomplete',
      select: 'Select',
      selectGroup: 'Select Group',
      selectV2: 'SelectV2',
      cascader: 'Cascader',
      switch: 'Switch',
      rate: 'Rate',
      colorPicker: 'Color Picker',
      transfer: 'Transfer',
      render: 'Render',
      radio: 'Radio',
      button: 'Button',
      checkbox: 'Checkbox',
      slider: 'Slider',
      datePicker: 'Date Picker',
      shortcuts: 'Shortcuts',
      today: 'Today',
      yesterday: 'Yesterday',
      aWeekAgo: 'A week ago',
      week: 'Week',
      year: 'Year',
      month: 'Month',
      dates: 'Dates',
      daterange: 'Date Range',
      monthrange: 'Month Range',
      dateTimePicker: 'DateTimePicker',
      dateTimerange: 'Datetime Range',
      timePicker: 'Time Picker',
      timeSelect: 'Time Select',
      inputPassword: 'input Password',
      passwordStrength: 'Password Strength',
      defaultForm: 'All examples',
      formDes:
        'The secondary encapsulation of base-form components based on ElementPlus realizes data-driven and supports all base-form parameters',
      example: 'example',
      operate: 'operate',
      change: 'Change',
      restore: 'Restore',
      disabled: 'Disabled',
      disablement: 'Disablement',
      delete: 'Delete',
      add: 'Add',
      setValue: 'Set value',
      resetValue: 'Reset value',
      set: 'Set',
      subitem: 'Subitem',
      formValidation: 'base-form validation',
      verifyReset: 'Verify reset'
    },

    levelDemo: {
      menu: 'Multi level app-menu cache',
      iframe: 'external page'
    },
    searchDemo: {
      dynamicOptions: 'Dynamic options'
    },
    tableDemo: {
      table: 'Table',
      tableDes: 'Secondary packaging of base-table components based on ElementPlus',
      index: 'Index',
      title: 'Title',
      author: 'Author',
      displayTime: 'Display time',
      importance: 'Importance',
      pageviews: 'Pageviews',
      action: 'Action',
      important: 'Important',
      good: 'Good',
      commonly: 'Commonly',
      operate: 'operate',
      example: 'example',
      show: 'Show',
      hidden: 'Hidden',
      pagination: 'pagination',
      reserveIndex: 'Reserve index',
      restoreIndex: 'Restore index',
      showSelections: 'Show selections',
      hiddenSelections: 'Restore selections',
      showExpandedRows: 'Show expanded rows',
      hiddenExpandedRows: 'Hidden expanded rows',
      changeTitle: 'Change title',
      header: 'Header',
      selectAllNone: 'Select all / none'
    }
  }
}

export default [
  // 登录接口
  {
    url: '/api/login',
    method: 'post',
    timeout,
    response: ({ body }) => {
      const data = body
      let hasUser = false
      for (const user of List) {
        if (user.username === data.username && user.password === data.password) {
          hasUser = true
          return {
            code: result_code,
            data: user,
            token: 'VGh1SnVuMTUwOToyMjoxNENTVDIwMjMjMTY4ODg=@'
          }
        }
      }
      if (!hasUser) {
        return {
          code: '500',
          message: '账号或密码错误'
        }
      }
    }
  },
  // 用户信息
  {
    url: '/api/getInfo',
    method: 'get',
    timeout,
    response: ({ headers }) => {
      return {
        code: headers.authorization ? result_code : 401, //模拟未登录
        message: headers.authorization ? '处理成功' : '未登录',
        data: {
          userId: '1373767183417344',
          userName: 'CHENJIAXING',
          perNo: 'CHENJIAXING',
          email: 'xxxxxx@xx.com',
          phone: null,
          tel: null,
          permissions: ['demo:form:create', 'demo:form:delete']
        }
      }
    }
  },
  // 退出接口
  {
    url: '/api/logout',
    method: 'get',
    timeout,
    response: () => {
      return {
        code: result_code,
        data: null
      }
    }
  },
  // 路由接口
  {
    url: '/api/getRouters',
    method: 'get',
    timeout,
    response: () => {
      return {
        code: result_code,
        data: routers
      }
    }
  },
  // 国际化资源
  {
    url: '/api/lang',
    method: 'get',
    timeout,
    response: ({ query }) => {
      return {
        code: result_code,
        data: i18n[query.lang] || {}
      }
    }
  }
] as MockMethod[]
